﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * 
 * */

using System.Threading;
using System.Windows.Forms;
using QWEntity.Local;
using QWEntity.Xlink;
using QWModel;
using QWRepository.Local;
using QWRepository.Xlink;
using QWTouchPanel.Properties;

namespace QWTouchPanel.Model
{
    /// <summary>
    /// QWSearch : UserControl
    /// </summary>
    public partial class QWSearch : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="QWSearch"/> class.
        /// </summary>
        public QWSearch()
        {
            InitializeComponent();

            // set BACKGROUND
            var t = new Thread(TBackgroundImage);
            t.Start();
        }

        /// <summary>
        /// Ts the background image.
        /// </summary>
        private void TBackgroundImage()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(TBackgroundImage);
                Invoke(o);
            }
            else
            {
                BackgroundImage = Resources.widget_black640;
            }
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton11 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton11_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text = "";
            lblHeader.Text = @"No. Permohonan:";
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton1_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text += ((QWBoxButton) sender).Text;
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton2_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text += ((QWBoxButton) sender).Text;
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton3_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text += ((QWBoxButton) sender).Text;
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton4 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton4_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text += ((QWBoxButton) sender).Text;
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton5 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton5_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text += ((QWBoxButton) sender).Text;
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton6 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton6_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text += ((QWBoxButton) sender).Text;
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton7 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton7_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text += ((QWBoxButton) sender).Text;
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton8 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton8_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text += ((QWBoxButton) sender).Text;
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton9 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton9_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text += ((QWBoxButton) sender).Text;
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton10 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton10_MouseDown(object sender, MouseEventArgs e)
        {
            lblDetail.Text += ((QWBoxButton) sender).Text;
        }

        /// <summary>
        /// Handles the MouseDown event of the qwBoxButton12 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwBoxButton12_MouseDown(object sender, MouseEventArgs e)
        {
            string teks = lblDetail.Text.Trim();
            if (string.IsNullOrEmpty(teks)) return;

            lblDetail.Text = teks.Substring(0, teks.Length - 1);
        }

        /// <summary>
        /// Handles the MouseUp event of the qwButton1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
        private void qwButton1_MouseUp(object sender, MouseEventArgs e)
        {
            var t = new Thread(SearchData);
            t.Start();
        }

        /// <summary>
        /// Searches the data.
        /// </summary>
        private void SearchData()
        {
            if (InvokeRequired)
            {
                var o = new DelegateThis(SearchData);
                Invoke(o);
            }
            else
            {
                string teks = lblDetail.Text.Trim();

                if (string.IsNullOrEmpty(teks)) return;

                XLPassportForm pass = new XLPassportFormBase().GetById(teks);
                string msg = "";

                if (pass != null)
                {
                    lblHeader.Text = string.Format("Permh. a.n {0}", pass.Name);

                    var db = new WorkflowBase();
                    Workflow wf = db.GetById(pass.WorkFlowXID.ToString().Trim());
                    db.Dispose();

                    msg = wf != null ? wf.WorkflowDescription : "Status tidak diketahui, hubungi petugas!";
                }
                else
                {
                    msg = "No. Permohonan tidak ditemukan!";
                }

                lblDetail.Text = msg;
            }
        }

        #region Nested type: DelegateThis

        /// <summary>
        /// DelegateThis
        /// </summary>
        private delegate void DelegateThis();

        #endregion

        private void btnLock_Click(object sender, System.EventArgs e)
        {
                Application.Restart();
        }

        

    }
}